package fun.yao.common.monitor.info;

import fun.yao.common.Constant;
import fun.yao.common.msg.HashtableMsg;
import fun.yao.common.uitls.LogUtil;

import java.io.File;
import java.math.BigDecimal;
import java.util.Hashtable;

/**
 * 磁盘信息
 * @author longjinglai
 *
 */
public class DiskInfo implements IMonitorInfo {

    /**总空间 单位M*/
    private BigDecimal total = new BigDecimal(0);

    /**使用空间 单位M*/
    private BigDecimal use = new BigDecimal(0);

    /**空闲空间 单位M*/
    private BigDecimal free = new BigDecimal(0);

    /**使用率 */
    private BigDecimal useRate = new BigDecimal(0);

    public BigDecimal getTotal() {
        return total;
    }

    public BigDecimal getUse() {
        return use;
    }

    public BigDecimal getFree() {
        return free;
    }

    public BigDecimal getUseRate()
    {
        return this.useRate;
    }

    public DiskInfo()
    {
        File file = new File(".");
        total = new BigDecimal(file.getTotalSpace()/1024.0/1024.0).setScale(2,BigDecimal.ROUND_HALF_UP);
        free = new BigDecimal(file.getFreeSpace()/1024.0/1024.0).setScale(2,BigDecimal.ROUND_HALF_UP);
        use = total.subtract(free);
        useRate = use.divide(total,2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     *
     * @param htm
     */
    public DiskInfo(HashtableMsg htm)
    {
        Hashtable<String, String> mapDisk = htm.get(0);
        if(mapDisk!=null)
        {
            this.total = new BigDecimal(mapDisk.get("total"));
            this.use = new BigDecimal(mapDisk.get("use"));
            this.free = new BigDecimal(mapDisk.get("free"));
            this.useRate = new BigDecimal(mapDisk.get("use_rate"));
        }
    }

    /**
     * 转换为HashtableMsg
     * @return
     */
    public HashtableMsg toHashtableMsg()
    {
        Hashtable<String, String> mapDisk = new Hashtable<String, String>();
        mapDisk.put("total", this.getTotal().toString());
        mapDisk.put("use", this.getUse().toString());
        mapDisk.put("free", this.getFree().toString());
        mapDisk.put("use_rate", this.getUseRate().toString());
        HashtableMsg htm = new HashtableMsg();
        htm.add(mapDisk);
        return htm;
    }

    @Override
    public String toString() {
        return "DiskInfo [free=" + free + ", total=" + total + ", use=" + use
                + ", useRate=" + useRate + "]";
    }

    public static void main(String[] args)
    {
        DiskInfo disk = new DiskInfo();
        System.out.println("总空间:" + disk.getTotal() + "M");
        System.out.println("使用空间:" + disk.getUse() + "M");
        System.out.println("空闲空间:" + disk.getFree() + "M");
        System.out.println("使用率:" + disk.getUseRate().multiply(new BigDecimal(100)) + "%");
    }

    public void print(StringBuilder sbOut) {
        try
        {
            sbOut.append("总空间："+this.getTotal()+"M，使用空间："+this.getUse()+"M，空闲空间："+this.getFree()+"M，使用率："+this.getUseRate().multiply(new BigDecimal(100))+"%\n");
        }catch (Exception e) {
            LogUtil.WriteLog(Constant.BADLY_ERROR, e);
        }
    }


}
